package com.ys.controller.api.storage;

import com.ys.config.MenuId;
import com.ys.entity.storage.TccLocation;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.storage.TccLocationService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 货位管理
 */
@Controller
@RequestMapping("/api/inter/tccLocation")
public class ApiInterTccLocationController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTccLocationController.class);

    @Autowired
    private CommonApiService<TccLocation> commonApiService;
    
    Long menuId = MenuId.TCC_Location;

    @Autowired
    private TccLocationService tccLocationService;

    /**
     * 弹出框-通用
     */
    @RequestMapping("/searchForModal")
    @ResponseBody
    public LayuiTableVoJA searchForModal(com.bo.annotation.Model model, HttpServletRequest request, HttpServletResponse response,
                                        @ModelAttribute TccLocation cond, String conditions) {
        cond
                .addLeftJoin("TCC_WarehouseArea B ON B.Ckq = A.Ckq")//库区
                .addLeftJoin("TCC_Warehouse C ON C.Cck = B.Cck")//仓库
                .addField("C.vck,B.vkq")
                .setStatus(1)
        ;

        LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
        return tableVoJA;

    }

    /**
     * 预约采购单子表信息(采购合同单调用)
     */
    @RequestMapping("/tccLocationRk")
    @ResponseBody
    public LayuiTableVoJA tccLocationRk(com.bo.annotation.Model model, HttpServletRequest request, HttpServletResponse response,
                                                       @ModelAttribute TccLocation cond, String conditions) {
        cond
                .addLeftJoin("TCC_WarehouseArea B ON B.Ckq = A.Ckq")//库区
                .addLeftJoin("TCC_Warehouse C ON C.Cck = B.Cck")//仓库
                .addField("C.vck,B.vkq")
                .setStatus(1)
                ;

        LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
        return tableVoJA;

    }
    /**
     * 设置查询的cond的sql设置
     */
    private TccLocation setSeachSql(TccLocation cond){
        cond.setOrderby("A.Id DESC")
                .addLeftJoin("TCC_WarehouseArea B ON B.Ckq=A.Ckq")
                .addLeftJoin("TCC_Warehouse C ON C.Cck=B.Cck")
                .addLeftJoin("TB_Country tb ON tb.code = C.countryCode")
                .addField("C.Cck cck,C.Vck vck,C.CountryCode countryCode,C.City city,tb.name countryName,B.Ckq ckq,B.Vkq vkq");

        return  cond;
    }

    @RequiresPermissions("tccLocation:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA searchDetail(@ModelAttribute TccLocation cond){

        cond = setSeachSql(cond);
        LayuiTableVoJA vo = commonApiService.searchLay(cond);
        return vo;
    }


    /*
     * 导出
     */
    @RequiresPermissions("tccLocation:export")
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TccLocation cond) throws Exception {
        try {
            cond.setOrderby("A.Id DESC");

            cond = setSeachSql(cond);
            cond.setPage(0).setSize(999999);
            cond = commonApiService.dataAuth(menuId, cond);
            //获取菜单字段数据
            List<Map<String, Object>> listData = cond.queryMap();

            List<YsCol> listField = new YsCol()
                    .setMenuId(menuId)
                    .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                    .setOrderby("A.SortNo asc")
                    .queryList();

            ExportExcelUtils<TccLocation> util = new ExportExcelUtils<TccLocation>(new TccLocation());

            String toFilePath = util.exportExcel(listData, listField, "货位管理");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        } catch (Exception e) {
            logger.error("导出出错：", e.getMessage(), e);
            return R.error("导出失败，请稍后重试！");
        }
    }
    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("货位新增传参：" + params);
            //修改
            return tccLocationService.add(params);
        } catch (Exception e) {
            logger.error("货位新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
    /**
     * 编辑
     */
    @PostMapping("/toEdit")
    @ResponseBody
    public R edit(@RequestParam Map<String, Object> params){
        try {
            return tccLocationService.edit(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
}
